#include "kservice/display.h"
#include "kernel/astralapi.h"
#include "ff.h"

static struct Display d;
static int cterminal_read(AstralDevice dev, char *buff, int buff_size, int *bytes_read)
{
    return 0;
}

static int cterminal_write(AstralDevice dev, const char *buff, int buff_size, int *bytes_write)
{
    for (int i = 0; i < buff_size; i++)
    {
        char c = *(buff + i);
        if (!c)
        {
            break;
        }
        display_char(&d, c, 0x0, 0x00ffffff);
    }
    return 0;
}

static int cterminal_open(AstralDevice dev, object arg)
{
    if (dev->status != OPENED)
    {
        dev->status = OPENED;
    }
    return 0;
}

static int cterminal_close(AstralDevice dev, object arg)
{
    return 0;
}

static struct AstralDriver cterminal_driver = {
    .dwrite = cterminal_write,
    .dread = cterminal_read,
    .dopen = cterminal_open,
    .dclose = cterminal_close,
    .description = "cterminal driver"};

void terminal_init()
{

    static unsigned vbuff[1024 * 1024];
    create_display(&d, NULL, vbuff, 1024 * 1024);
    set_display_attr(&d, 800, 760, 102);
    draw_bitmap(&d, "/astral.bmp", 0, 0);
    switch_display(&d);

    u32 model = allocate_dynamic_mid();
    struct AstralDevInfo t = {
        .name = "terminal"};
    t.mid = model;
    static struct AstralDevice tdev;
    register_driver(model, &cterminal_driver);
    register_device(&tdev, &t);
}
